package com.zhudi.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhudi.expection.BusinessException;
import com.zhudi.model.dto.InterfaceModel;
import com.zhudi.model.entity.User;
import com.zhudi.model.vo.UserVO;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.stream.Collectors;

import static com.zhudi.commen.ErrorCode.PARAMS_ERROR;

public class BeanUtils {

    public static Page<UserVO> PageUserToUserVO(Page<User> userPage){
        Page<UserVO> userVOResult = new Page<>();
        org.springframework.beans.BeanUtils.copyProperties(userPage, userVOResult);
        // 将Page<User>转换为Page<UserVO>
        List<UserVO> userVOList = userPage.getRecords().stream()
                .map(user -> {
                    UserVO userVO = new UserVO();
                    org.springframework.beans.BeanUtils.copyProperties(user, userVO);
                    return userVO;
                })
                .collect(Collectors.toList());

        userVOResult.setRecords(userVOList);
        return userVOResult;
    }

    public static <T, S> Page<S> mapEntityPageToVoPage(Page<T> entityPage, Class<S> voClass) {
        List<S> voList = entityPage.getRecords().stream()
                .map(entity -> {
                    S vo = null;
                    try {
                        vo = voClass.getDeclaredConstructor().newInstance();
                        org.springframework.beans.BeanUtils.copyProperties(entity, vo);
                    } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
                        throw new RuntimeException("Unable to create VO object", e);
                    }
                    return vo;
                })
                .collect(Collectors.toList());
        Page<S> voResult = new Page<>();
        org.springframework.beans.BeanUtils.copyProperties(entityPage, voResult);
        voResult.setRecords(voList);
        // 使用 PageImpl 而不是直接 new Page<S>(); 创建分页结果
        return voResult;
    }

    //todo 将来有需要再将这个方法抽取成一个通用的方法，抽取一下InterfaceModel的父类
    public static void generateClass(String templatePath, String classOutputPath,String templateName, InterfaceModel interfaceModel) throws IOException, TemplateException {
        if (interfaceModel==null){
            throw new BusinessException(PARAMS_ERROR);
        }
        //new出Configuration对象，参数为FreeMarker版本号
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);

        //指定模板文件所在的路径
        configuration.setDirectoryForTemplateLoading(new File(templatePath));
        //设置模板文件使用的字符集
        configuration.setDefaultEncoding("utf-8");
        //创建模板对象，加载指定模板
        Template template = configuration.getTemplate(templateName);
        //创建数据模型
        Writer out  = new FileWriter(classOutputPath+"/"+interfaceModel.getClassName()+"Request.java");

        template.process(interfaceModel, out);
        //生成文件后别忘了关闭哦
        out.close();
    }


}
